<template>
    <div class="EditEducationPopup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit" @close="handleClose">
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
				<el-form-item label="学历" prop="Name">
				    <el-input v-model="formData.Name" placeholder="请输入学历名称" clearable />
				</el-form-item>

                <el-form-item label="证书编号" prop="CertificateNumber">
                    <el-input v-model="formData.CertificateNumber" placeholder="请输入证书编号" clearable />
                </el-form-item>
				<el-form-item label="入学日期" prop="JoinDate">
                    <el-date-picker v-model="formData.JoinDate" type="date" />
                </el-form-item>
				<el-form-item label="毕业日期" prop="GraduationDate">
                    <el-date-picker v-model="formData.GraduationDate" type="date" />
                </el-form-item>
                <el-form-item label="学年制" prop="Years">
                    <el-select class="w-[280px]" v-model="formData.Years">
                        <el-option v-for="(item, key) in optionData.Years" :key="key" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="学校名称" prop="SchoolName">
                    <el-input v-model="formData.SchoolName" clearable />
                </el-form-item> 
				<el-form-item label="专业" prop="Professional">
				    <el-input v-model="formData.Professional" clearable />
				</el-form-item> 
				<el-form-item label="完成状态" prop="CompletionStatus">
				    <el-input v-model="formData.CompletionStatus" clearable />
				</el-form-item> 
				<el-form-item label="学校类别" prop="Years">
                    <el-select class="w-[280px]" v-model="formData.SchoolCategory">
                        <el-option v-for="(item, key) in optionData.SchoolCategory" :key="key" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
				<el-form-item label="单位类别" prop="SchoolType">
				    <el-input v-model="formData.SchoolType" clearable />
				</el-form-item> 
				<el-form-item label="学校形式" prop="Years">
                    <el-select class="w-[280px]" v-model="formData.LearnStyle">
                        <el-option v-for="(item, key) in optionData.LearnStyle" :key="key" :label="item.name"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
				<el-form-item label="学历说明" prop="EducationInstructions">
				    <el-input v-model="formData.EducationInstructions" clearable />
				</el-form-item> 
				
				<el-form-item label="说明/备注" prop="Remark">
				    <el-input v-model="formData.Remark" clearable />
				</el-form-item> 
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup name="EditEducationPopup">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import { useDictData } from '@/hooks/useDictOptions'
import { EditEducations } from '@/api/consumer'
const { dictData: optionData } = useDictData('Years,SchoolCategory,LearnStyle')
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑学历信息' : '添加学历信息'
})
//console.log('数据列表',optionData)
const props = defineProps(['member_id']) /**从view.vue传递参数 */
const formData = reactive({
    id: 0,
    member_id: props.member_id,
     Name: '',
     CertificateNumber: '',
     JoinDate: '',
     GraduationDate: '',
     Years: '',
     SchoolName: '',Professional: '',
	 SchoolCategory: '',SchoolType: '',LearnStyle: '',EducationInstructions: '',Status: '',Remark: '',
     CompletionStatus: ''
})

const formRules = reactive({
     Name: [
         {
            required: true,
             message: '请输入学历',
             trigger: ['blur']
         }
     ],
	 JoinDate: [
	     {
	         required: true,
	         message: '请选择入学日期',
	         trigger: ['blur']
	     }
	 ],
	 GraduationDate: [
	     {
	         required: true,
	         message: '请选择毕业日期',
	         trigger: ['blur']
	     }
	 ],
     SchoolName: [
         {
             required: true,
             message: '请输入学校名称',
             trigger: ['blur']
         }
     ],
     LearnStyle: [
        {
            required: true,
             message: '请输入学习形式',
             trigger: ['blur']
         }
     ]
     
})


const handleSubmit = async () => {
    await formRef.value?.validate()
    console.log('学历表单内容', formData)
    await EditEducations(formData)
    //mode.value == 'edit' ? await adminEdit(formData) : await adminAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (row: any) => {
    formData.id = row.id
    const data = await EditEducations({
        id: row.id,
    		 mode:'get'
    })
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            formData[key] = data[key]
        }
    }
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData
})
</script>
